<template>
  <div>
    <div class="modules">
      <el-row :gutter="10">
        <el-col :span="6">
          <h3>资源开关</h3>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="6" v-for="m in moduleList" :key="m.id">
          模块
          <el-switch
            v-model="m.switch_status"
            @change="ChangeModuleStatus(m.id)"
            :inactive-text="m.module_name"
          >
          </el-switch>
        </el-col>
      </el-row>
      <el-row :gutter="15" style="margin-top:30px">
        <el-col :span="6">
          <el-switch
            v-model="shutDownStatus"
            @change="ShutDownServer"
            inactive-text="关闭服务端"
          >
          </el-switch>
        </el-col>
      </el-row>
      <el-row :gutter="15" style="margin-top:30px">
        <el-col :span="6">
          <span>服务器过期时间: {{ ExpirationTime }}</span>
        </el-col>
      </el-row>
      <el-row :gutter="15" style="margin-top:30px">
        <el-col span="6">
          <el-input v-model="licence" placeholder="请输入许可证"></el-input>
        </el-col>
        <el-col>
          <el-button
            @click="setLicence"
            type="primary"
            size="medium"
            plain
            round
            >发送许可证</el-button
          >
        </el-col>
      </el-row>
      <el-row> </el-row>
    </div>
  </div>
</template>

<script>
export default {
  props: {},
  components: {},
  data() {
    return {
      moduleList: [],
      shutDownStatus: false,
      ExpirationTime: null, // 许可证过期时间
      licence: null
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.$nextTick(() => {
      // 获取模块开关
      this.getModuleList()
      // 获取许可证到期时间
      this.getExpiration()
    })
  },
  methods: {
    // 获取模块列表
    async getModuleList() {
      const res = await this.$API.setting.getModuleList()
      if (res.status == 200) {
        this.moduleList = res.data
      }
    },
    // 改变模块状态
    async ChangeModuleStatus(id) {
      console.log(status)
      let datas = new FormData()
      datas.append('id', id)
      const res = await this.$API.setting.changeSwitch(datas)
      if (res.status == 200) {
        this.$message.success('设置模块状态成功')
        // 重新获取模块状态
        this.getModuleList()
      } else {
        this.$message.error(res.msg)
      }
    },
    // 关闭服务端
    async ShutDownServer() {
      const res = await this.$API.setting.shutDownServer()
    },
    // 获取服务器到期时间
    async getExpiration() {
      const res = await this.$API.setting.getLicenceExpiraion()
      console.log(res)
      if (res.status == 200) {
        this.ExpirationTime = res.data
      }
    },
    // 设置许可证
    async setLicence() {
      let formData = new FormData()
      formData.append('licence', this.licence)
      const res = await this.$API.setting.setLicence(formData)
      if (res.status == 200) {
        window.location.reload()
        
      }
    }
  }
}
</script>

<style scoped>
.modules {
  width: 100%;
  padding: 20px;
}
.el-col {
  margin-bottom: 20px;
}
</style>
